% extract_conv_case0_convergence.m
% 从Conv_Case0日志文件中提取收敛数据
% 作者: cxy
% 日期: 2025年

function [phase1_data, phase2_data, overall_data] = extract_conv_case0_convergence()
    % 读取日志文件
    log_file = 'result/Conv_Case0/收敛conv_case0.txt';
    
    if ~exist(log_file, 'file')
        error('日志文件不存在: %s', log_file);
    end
    
    % 读取所有行
    fid = fopen(log_file, 'r', 'n', 'UTF-8');
    lines = {};
    while ~feof(fid)
        line = fgetl(fid);
        if ischar(line)
            lines{end+1} = line;
        end
    end
    fclose(fid);
    
    fprintf('读取到 %d 行日志数据\n', length(lines));
    
    % 初始化数据存储
    phase1_iterations = [];
    phase1_scores = [];
    phase2_improvements = [];
    
    % 解析阶段一数据
    fprintf('解析阶段一收敛数据...\n');
    phase1_pattern = '阶段一 > 迭代 (\d+) / \d+: 发现.*新分数为: (\d+)';
    
    for i = 1:length(lines)
        line = lines{i};
        matches = regexp(line, phase1_pattern, 'tokens');
        if ~isempty(matches)
            iteration = str2double(matches{1}{1});
            score = str2double(matches{1}{2});
            phase1_iterations = [phase1_iterations; iteration];
            phase1_scores = [phase1_scores; score];
        end
    end
    
    % 添加初始点 (从日志中提取的关键信息)
    initial_score = 133031;  % 从日志末尾提取
    phase1_after_score = 39154;  % 阶段一完成后的分数
    final_score = 29765;  % 最终分数
    
    % 阶段一数据（添加初始点）
    phase1_iterations = [0; phase1_iterations];
    phase1_scores = [initial_score; phase1_scores];
    
    fprintf('阶段一找到 %d 个改进点\n', length(phase1_iterations)-1);
    
    % 阶段二数据（简化处理）
    % 由于阶段二的ILS结构复杂，我们只记录关键改进点
    phase2_iterations = [0; 1];  % 0=阶段一结束，1=阶段二结束
    phase2_scores = [phase1_after_score; final_score];
    
    fprintf('阶段二: %d -> %d (改进 %d)\n', phase1_after_score, final_score, phase1_after_score - final_score);
    
    % 构建输出数据结构
    phase1_data = struct();
    phase1_data.iteration = phase1_iterations;
    phase1_data.score = phase1_scores;
    phase1_data.phase = ones(size(phase1_iterations));
    
    phase2_data = struct();
    phase2_data.iteration = phase2_iterations;
    phase2_data.score = phase2_scores;
    phase2_data.phase = 2 * ones(size(phase2_iterations));
    
    % 整体收敛数据
    overall_data = struct();
    overall_data.iteration = [phase1_iterations; phase2_iterations(2)];  % 避免重复阶段一结束点
    overall_data.score = [phase1_scores; final_score];
    overall_data.phase = [ones(size(phase1_iterations)); 2];
    
    % 保存为CSV文件（与其他案例格式一致）
    output_dir = 'result/Conv_Case0';
    if ~exist(output_dir, 'dir')
        mkdir(output_dir);
    end
    
    % 阶段1收敛文件
    phase1_file = fullfile(output_dir, 'Conv_Case0_phase1_convergence.csv');
    writematrix([phase1_data.iteration, phase1_data.score], phase1_file, ...
        'WriteMode', 'overwrite');
    
    % 写入表头
    fid = fopen(phase1_file, 'w');
    fprintf(fid, 'iteration,score\n');
    for i = 1:length(phase1_data.iteration)
        fprintf(fid, '%d,%d\n', phase1_data.iteration(i), phase1_data.score(i));
    end
    fclose(fid);
    
    % 阶段2收敛文件
    phase2_file = fullfile(output_dir, 'Conv_Case0_phase2_convergence.csv');
    fid = fopen(phase2_file, 'w');
    fprintf(fid, 'iteration,score\n');
    for i = 1:length(phase2_data.iteration)
        fprintf(fid, '%d,%d\n', phase2_data.iteration(i), phase2_data.score(i));
    end
    fclose(fid);
    
    % 整体收敛文件
    overall_file = fullfile(output_dir, 'Conv_Case0_overall_convergence.csv');
    fid = fopen(overall_file, 'w');
    fprintf(fid, 'phase,iteration,score\n');
    for i = 1:length(overall_data.iteration)
        fprintf(fid, '%d,%d,%d\n', overall_data.phase(i), overall_data.iteration(i), overall_data.score(i));
    end
    fclose(fid);
    
    fprintf('\n=== 收敛数据提取完成 ===\n');
    fprintf('阶段1改进: %d -> %d (%.2f%%)\n', initial_score, phase1_after_score, ...
        100*(initial_score-phase1_after_score)/initial_score);
    fprintf('阶段2改进: %d -> %d (%.2f%%)\n', phase1_after_score, final_score, ...
        100*(phase1_after_score-final_score)/phase1_after_score);
    fprintf('总体改进: %d -> %d (%.2f%%)\n', initial_score, final_score, ...
        100*(initial_score-final_score)/initial_score);
    
    fprintf('\n生成的文件:\n');
    fprintf('  - %s\n', phase1_file);
    fprintf('  - %s\n', phase2_file);
    fprintf('  - %s\n', overall_file);
end
